﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.segments.field;
using ConsoleApplication1.segments.data_type;
namespace ConsoleApplication1.segments
{
    class DG1
    {
        private StringBuilder ID_dignosis;
        private StringBuilder Diagnosis_coding_method;
        private CE Diagnosis_code;
        private StringBuilder Diagnosis_description;
        private StringBuilder Diagnosis_date_time;
        public StringBuilder Diagnosis_type { get; set; }

        public StringBuilder id_dignosis { get; set; }
        public StringBuilder diagnosis_code { get; set; }
        public StringBuilder diagnosis_description { get; set; }
        public StringBuilder diagnosis_date_time { get; set; }

        public CE Major_dignostic_category { get; set; }
        public CE dignostic_related_group { get; set; }
        public StringBuilder DRG_approval_indicator { get; set; }
        public StringBuilder group_review_code { get; set; }
        public StringBuilder outlier_type { get; set; }
        public StringBuilder outlier_days { get; set; }
        public StringBuilder outlier_cost { get; set; }
        public StringBuilder group_version_and_cost { get; set; }
        public StringBuilder Diagnosis__DRG_priority { get; set; }
        public CN Diagnosing_clinician { get; set; }
        public bool required { get; set; }

        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }


        public int fill(string source, int temp)
        {
            int i, x = 0;
            for (i = temp; i < source.Length; i++)
            {
                if (source[i] == '|')
                {
                    if (check(source, i) != null)
                    {
                        ID_dignosis.Capacity = ID_dignosis.Length;
                        diagnosis_description.Capacity = diagnosis_description.Length;
                        diagnosis_date_time.Capacity = diagnosis_date_time.Length;
                        DRG_approval_indicator.Capacity = DRG_approval_indicator.Length;
                        group_review_code.Capacity = group_review_code.Length;
                        outlier_type.Capacity = outlier_type.Length;
                        outlier_days.Capacity = outlier_days.Length;
                        outlier_cost.Capacity = outlier_cost.Length;
                        group_version_and_cost.Capacity = group_version_and_cost.Length;
                        return i - 1;
                    }
                   else if (x == 14)
                    {
                        ID_dignosis.Capacity = ID_dignosis.Length;
                        diagnosis_description.Capacity = diagnosis_description.Length;
                        diagnosis_date_time.Capacity = diagnosis_date_time.Length;
                        DRG_approval_indicator.Capacity = DRG_approval_indicator.Length;
                        group_review_code.Capacity = group_review_code.Length;
                        outlier_type.Capacity = outlier_type.Length;
                        outlier_days.Capacity = outlier_days.Length;
                        outlier_cost.Capacity = outlier_cost.Length;
                        group_version_and_cost.Capacity = group_version_and_cost.Length;
                        return i;
                    }
                    else
                    {
                        x++;
                        continue;
                    }
                }
                else
                {
                    if (x == 1)  // passing prameter of set id - PID
                    {
                        if (source[i] != '|')
                        {
                            if (id_dignosis == null)
                            id_dignosis = new StringBuilder();
                            id_dignosis.Insert(id_dignosis.Length, source[i]);
                        }
                    }
                    else if (x == 2)
                    {
                        if (source[i] != '|')
                        {
                            if (Diagnosis_coding_method == null)
                                Diagnosis_coding_method = new StringBuilder();
                            Diagnosis_coding_method.Insert(Diagnosis_coding_method.Length, source[i]);
                        }
                    }
                    else if (x == 3)
                    {
                        if (source[i] != '|')
                        {
                            if (Diagnosis_code == null)
                                Diagnosis_code = new  CE();
                            i=Diagnosis_code.fill(source, i);
                        }
                    }
                    else if (x == 4 )
                    {
                        if (source[i] != '|')
                        {
                            if (Diagnosis_description == null)
                                Diagnosis_description=new StringBuilder();
                            Diagnosis_description.Insert(Diagnosis_description.Length, source[i]);
                        }
                    }
                    else if (x == 5)
                    {
                        if (source[i] != '|')
                        {
                            if (Diagnosis_date_time == null)
                                Diagnosis_date_time = new StringBuilder();
                            Diagnosis_date_time.Insert(Diagnosis_date_time.Length, source[i]);
                        }
                    }
                    else if (x == 6)
                    {
                        if (source[i] != '|')
                        {
                            if (Diagnosis_type == null)
                                Diagnosis_type = new StringBuilder();
                            Diagnosis_type.Insert(Diagnosis_type.Length, source[i]);
                        }
                    }
                    else if (x == 7)
                    {
                        if (source[i] != '|')
                        {
                            if (Major_dignostic_category == null)
                                Major_dignostic_category = new CE();
                           i= Major_dignostic_category.fill(source, i);
                        }
                    }
                    else if (x == 8)
                    {
                        if (source[i] != '|')
                        {
                            if (dignostic_related_group == null)
                                dignostic_related_group = new CE();
                            i=dignostic_related_group.fill(source, i);
                        }
                    }
                    else if (x == 9)
                    {
                        if (source[i] != '|')
                        {
                            if (DRG_approval_indicator == null)
                                DRG_approval_indicator = new  StringBuilder();
                            DRG_approval_indicator.Insert(DRG_approval_indicator.Length, source[i]);
                        }
                    }

                    else if (x == 10)
                    {
                        if (source[i] != '|')
                        {
                            if (group_review_code == null)
                                group_review_code = new StringBuilder();
                            group_review_code.Insert(group_review_code.Length, source[i]);
                        }
                    }

                    else if (x == 11)
                    {
                        if (source[i] != '|')
                        {
                            if (outlier_type == null)
                                outlier_type = new StringBuilder();
                            outlier_type.Insert(outlier_type.Length, source[i]);
                        }
                    }
                    else if (x == 12)
                    {
                        if (source[i] != '|')
                        {
                            if (outlier_days == null)
                                outlier_days = new StringBuilder();
                            outlier_days.Insert(outlier_days.Length, source[i]);
                        }
                    }
                    else if (x == 13)
                    {
                        if (source[i] != '|')
                        {
                            if (outlier_cost == null)
                                outlier_cost = new StringBuilder();
                            outlier_cost.Insert(outlier_cost.Length, source[i]);
                        }
                    }
                    else if (x == 14)
                    {                        
                        if (source[i] != '|')
                        {
                            if (group_version_and_cost == null)
                                group_version_and_cost = new StringBuilder();
                            group_version_and_cost.Insert(group_version_and_cost.Length, source[i]);
                        }
                    }
                    else if (x == 15)
                    {
                        if (source[i] != '|')
                        {
                            if (Diagnosis__DRG_priority == null)
                                Diagnosis__DRG_priority = new StringBuilder();
                            Diagnosis__DRG_priority.Insert(Diagnosis__DRG_priority.Length, source[i]);
                        }
                    }
                    else if (x == 16)
                    {
                        if (source[i] != '|')
                        {
                            if (Diagnosing_clinician == null)
                                Diagnosing_clinician = new  CN ();
                            i = Diagnosing_clinician.fill(source, i);
                            return i;
                        }
                    }
                }
            }
            return i;
        }



        public override string ToString()
        {
            string dg1_string = "DG1";

            if (ID_dignosis.Length != 0)
                dg1_string += "|" + ID_dignosis;
            if (Diagnosis_coding_method.Length != 0)
                dg1_string += "|" + Diagnosis_coding_method;
            if (Diagnosis_code != null)
                dg1_string += "|" + Diagnosis_code.tostring();
            if (Diagnosis_description.Length != 0)
                dg1_string += "|" + Diagnosis_description;
            if (Diagnosis_date_time.Length != 0)
                dg1_string += "|" + Diagnosis_date_time;
            if (Diagnosis_type.Length != 0)
                dg1_string += "|" + Diagnosis_type;
            if (Major_dignostic_category != null)
                dg1_string += "|" + Major_dignostic_category.tostring();
            if (dignostic_related_group != null)
                dg1_string += "|" + dignostic_related_group.tostring();
            if (DRG_approval_indicator.Length != 0)
                dg1_string += "|" + DRG_approval_indicator;
            if (group_review_code.Length != 0)
                dg1_string += "|" + group_review_code;
            if (outlier_type.Length != 0)
                dg1_string += "|" + outlier_type;
            if (outlier_days.Length != 0)
                dg1_string += "|" + outlier_days;
            if (outlier_cost.Length != 0)
                dg1_string += "|" + outlier_cost;
            if (group_version_and_cost.Length != 0)
                dg1_string += "|" + group_version_and_cost;
            if (Diagnosis__DRG_priority.Length != 0)
                dg1_string += "|" + Diagnosis__DRG_priority;
            if (Diagnosing_clinician != null)
                dg1_string += "|" + Diagnosing_clinician.ToString();

            return dg1_string;
        }


                                           
    }
}
